package com.mrent.service;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.springframework.stereotype.Component;

import com.mrent.entity.Picture;

@Component
public class PictureServiceImpl implements PictureService {
	
	/**
	 * 保存图片.
	 * 
	 * @param picture
	 */
	public void savePicture(Picture picture){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.persist(picture);
		}catch(Exception e){
			System.err.println("异常问题");
			e.printStackTrace();
		}
		finally {
			trans.commit();
		}
	}
	
	/**
	 * 获取图片.
	 * 
	 * @param pictureId
	 * @return
	 */
	public Picture getPicture(Long pictureId){
		EntityManager em = EMF.getEM();
		Picture pic = null;
		try{
			pic = em.find(Picture.class, pictureId);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			
		}
		return pic;
	}
	
	/**
	 * 删除 图片.
	 * 
	 * @param pictureId
	 */
	public void delPicture(Long pictureId){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em .getTransaction();
		try{
			trans.begin();
			Picture pic = em.find(Picture.class, pictureId);
			if(pic != null){
				em.remove(pic);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			trans.commit();
		}
	}

}
